package com.xy.datastruc.graph;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

public class LinkGraph {
    List<Vertex> vertices;
    //List<Vertex> 可优化
    HashMap<String, List<Vertex>> adgeinfo;

    public  LinkGraph() {

        vertices = new ArrayList<>();
        adgeinfo = new HashMap<>();

    }

    public void addVertex(Vertex vertex) {
        vertices.add(vertex);
        adgeinfo.put(vertex.value, new ArrayList<>());
    }



    public void addAdge(String start, String end) {
        int startindex=0;
        for (int i=0;i<vertices.size();i++){
            if (start.equals(vertices.get(i).value)){
                startindex=i;
                break;
            }
        }
        int endindex=0;
        for (int i=0;i<vertices.size();i++){
            if (end.equals(vertices.get(i).value)){
                endindex=i;
                break;
            }
        }


        List<Vertex> startVertices = adgeinfo.get(start);
        if (startVertices==null){
            startVertices=new ArrayList<>();
            adgeinfo.put(start,startVertices);

        }
        List<Vertex> endVertices = adgeinfo.get(start);
        if (endVertices==null){
            endVertices=new ArrayList<>();
            adgeinfo.put(end,endVertices);

        }

        startVertices.add(vertices.get(endindex));
     }


    public void printGraph(){
        for (int i=0;i<vertices.size();i++){
             String vertex=vertices.get(i).value;
             String nextVertexlist=  adgeinfo.get(vertex).toString();
             System.out.println(vertex+"  "+nextVertexlist);
        }
    }
}
